﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using Axis.Dao.Order;
using Axis.BusinessObject.Core;
using System.Web.Script.Serialization;
using Axis.BusinessObject.Product;
using Axis.Dao.Product;
using Axis.Web.Administrator.Global;

namespace Sotipha.Web.Views.Cart.Service
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    [System.Web.Script.Services.ScriptService]
    public class OrderServer : System.Web.Services.WebService
    {
        [WebMethod(Description = "Per session Hit Counter", EnableSession = true)]
        public string SaveOrder(string CustomerName, string Phone, string Address, string Email)
        {
            Session["CustomerName"] = CustomerName;
            Session["Phone"] = Phone;
            Session["Address"] = Address;
            Session["Email"] = Email;

            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("Root");
            XmlElement InputValue = doc.CreateElement("InputValue");
            root.AppendChild(InputValue);
            //InputValue.SetAttribute("UserID", CSecurity.USER_ID.ToString());
            //InputValue.SetAttribute("SecrectToken", CSecurity.SECRECT_TOKEN.ToString());
            InputValue.SetAttribute("CustomerName", CustomerName);
            InputValue.SetAttribute("Phone", Phone);
            InputValue.SetAttribute("Address", Address);
            InputValue.SetAttribute("Email", Email);
            InputValue.SetAttribute("Action", "INSERT");
            if (Session["OrderID"] != null)
            {
                InputValue.SetAttribute("Action", "UPDATE");
                InputValue.SetAttribute("ID", Session["OrderID"].ToString());
            }
            string pattem = "<InputExtend ProductID=\"{0}\" Quantity=\"{1}\" Price=\"{2}\" />";
            string InputExtend = "";
            var items = (List<ShoppingCart>)Session["ShoppingCart"];
            foreach (ShoppingCart shoppingCart in items)
            {
                InputExtend += string.Format(pattem, shoppingCart.product.ID, shoppingCart.quality, shoppingCart.product.Price);
            }

            COrderDao orderDao = new COrderDao();
            CApplicationMessage message = orderDao.ActionCustomerOrder(InputValue.OuterXml + InputExtend);
            if (message.IsSuccessfull == true)
            {
                Session["OrderID"] = message.Result;
                string fileName = "Order_VN";
                string Subject = "XÁC NHẬN ĐƠN ĐẶT HÀNG ";
                if (Session["lang"].ToString() == "en")
                {
                    fileName = "Order_EN";
                    Subject = "PURCHASE ORDER ACKNOWLEDGMENT ";
                }
                CEmail objEmail = new CEmail();
                objEmail.SendMail(Email, Subject, fileName);
            }
            string output = new JavaScriptSerializer().Serialize(message);
            return output;
        }
        [WebMethod(Description = "Per session Hit Counter", EnableSession = true)]
        public string SendToFriend(string Email, string Link)
        {
            CEmail objEmail = new CEmail();
            objEmail.SendMail(Email, "Sotipha.com.vn", Link);
            return "";
        }
    }

}
